function fm_clock(varargin)
% FM_CLOCK create a clock
%
%Author:    Federico Milano
%Date:      11-Nov-2002
%Update:    10-Feb-2003
%Version:   1.0.2
%
%E-mail:    Federico.Milano@uclm.es
%Web-site:  http://www.uclm.es/area/gsee/Web/Federico
%
% Copyright (C) 2002-2013 Federico Milano

global Fig Settings Theme

if nargin
  switch varargin{1}
   case 'keyesc'

    tasto = get(gcf,'CurrentCharacter');
    if (double(tasto) == 13 || double(tasto) == 27),
      delete(gcf);
    end

  end
  return
end

if isunix
  fontname = 'Times';
else
  fontname = 'Times New Roman';
end

month = {'Jan';'Feb';'Mar';'Apr';'May';'Jun';'Jul';'Aug';'Sep';'Oct';'Nov';'Dec'};
a = clock;

if Fig.clock ~= 0, return, end

h0 = figure('Color',Theme.color01, ...
            'Units', 'normalized', ...
            'Colormap',[], ...
            'CreateFcn','Fig.clock = gcf;', ...
            'DeleteFcn','close(Fig.clock)', ...
            'KeyPressFcn','fm_clock keyesc', ...
            'FileName','fm_clock', ...
            'MenuBar','none', ...
            'Name','Clock', ...
            'NumberTitle','off', ...
            'PaperPosition',[18 180 576 432], ...
            'PaperType','A4', ...
            'PaperUnits','points', ...
            'Position',sizefig(0.2450,0.3333), ...
            'Userdata', 1, ...
            'Resize','on', ...
            'ToolBar','none', ...
            'WindowButtonDownFcn','set(Fig.clock,''Userdata'',-1)');
h1 = axes('Parent',h0, ...
          'Box','on', ...
          'CameraUpVector',[0 1 0], ...
          'CameraUpVectorMode','manual', ...
          'Color',Theme.color04, ...
          'ColorOrder',Settings.color, ...
          'Layer','top', ...
          'Position',[0.1 0.1 0.8 0.8], ...
          'Tag','Axes1', ...
          'XColor',[0 0.251 0.502], ...
          'XLim',[0.5 200.5], ...
          'XLimMode','manual', ...
          'XTick',[], ...
          'XTickLabelMode','manual', ...
          'YColor',[0 0.251 0.502], ...
          'YDir','reverse', ...
          'YLim',[0.5 167.5], ...
          'YLimMode','manual', ...
          'YTickLabelMode','manual', ...
          'YTick',[], ...
          'ZColor',[0 0 0]);
hdata = uicontrol('Parent',h0, ...
                  'Units', 'normalized', ...
                  'BackgroundColor',Theme.color01, ...
                  'Enable','inactive', ...
                  'FontName',fontname, ...
                  'FontSize',10, ...
                  'FontWeight','bold', ...
                  'ForegroundColor',Theme.color05, ...
                  'Position',[0.65 0.0 0.3  0.1], ...
                  'String',[num2str(a(3)),' ',month{a(2)},' ',num2str(a(1))], ...
                  'Style','text', ...
                  'Tag','TextDate');

day = a(3);
t = 0:0.01:2*pi+0.01;
x = cos(t);
y = sin(t);
plot(x,y,'k');
patch(x,y,Theme.color04)
hold on
plot(0.035*x,0.035*y,'k');
h = text(0,0.775*sin(pi/6),'PSAT');
set(h, ...
    'FontName',fontname, ...
    'FontSize',10, ...
    'FontAngle','oblique', ...
    'FontWeight','bold', ...
    'HorizontalAlignment','center')

for i = 1:12
  ang = pi/2-pi*i/6;
  h = plot([0.95*cos(ang)  cos(ang)], ...
           [0.95*sin(ang)  sin(ang)], 'k');
  set(h,'LineWidth',2)
  xt = 0.775*cos(ang);
  yt = 0.775*sin(ang);
  ht = text(xt,yt,num2str(i));
  set(ht, ...
      'FontName',fontname, ...
      'FontSize',12, ...
      'FontAngle','oblique', ...
      'FontWeight','bold', ...
      'HorizontalAlignment','center')
end
for i = 1:60
  ang = pi*i/30;
  plot([0.95*cos(ang)  cos(ang)], ...
       [0.95*sin(ang)  sin(ang)],'k')
end

plot(0.95*x,0.95*y,'k');

t1 = pi/4:0.01:3*pi/4;
t2 = 3*pi/4:-0.01:pi/4;
hx = 0.05+1.2*(cos(pi/4)+[cos(t1), cos(t2)])/3;
hy = 0.07*[sin(t1)-sin(pi/4),sin(pi/4)-sin(t2)];
mx = 0.05+2*(cos(pi/4)+[cos(t1), cos(t2)])/3;
my = 0.05*[sin(t1)-sin(pi/4),sin(pi/4)-sin(t2)];

s = pi/2-pi/30*a(6);
b = a(6)/60;
if b == 1; b = 0; end
m = pi/2-pi/30*(a(5)+b);
c = a(5)/60;
if c == 1; c = 0; end
o = pi/2-pi/6*(a(4)+c);
hh = plot([hx*cos(o)-hy*sin(o)], ...
          [hx*sin(o)+hy*cos(o)]);
hm = plot([mx*cos(m)-my*sin(m)], ...
          [mx*sin(m)+my*cos(m)]);
hs1 = plot([0.05*cos(s) 0.98*cos(s)], ...
           [0.05*sin(s) 0.98*sin(s)]);
hs2 = plot([0.05*cos(pi+s) 0.2*cos(pi+s)], ...
           [0.05*sin(pi+s) 0.2*sin(pi+s)]);
set(hh, ...
    'EraseMode','xor', ...
    'Color',Theme.color11)
set(hm, ...
    'EraseMode','xor', ...
    'Color',Theme.color11)
set(hs1, ...
    'EraseMode','xor', ...
    'Color',[0.5 0 0.5])
set(hs2, ...
    'EraseMode','xor', ...
    'Color',[0.5 0 0.5])
set(gca, ...
    'Visible','off', ...
    'Drawmode','fast', ...
    'NextPlot','add')
counter = 0;

while 1
  try
    a = get(Fig.clock,'Userdata');
  catch
    Fig.clock = 0;
    break
  end
  if a == -1,
    delete(Fig.clock)
    Fig.clock = 0;
    break
  end
  a = clock;
  if a(3) ~= day
    day = a(3);
    set(hdata, ...
        'String',[num2str(a(3)),' ', ...
                  month{a(2)},' ', ...
                  num2str(a(1))])
  end
  s = pi/2-pi/30*a(6);
  b = a(6)/60;
  if b == 1, b = 0; end
  m = pi/2-pi/30*(a(5)+b);
  c = a(5)/60;
  if c == 1, c = 0; end
  o = pi/2-pi/6*(a(4)+c);
  try
    if counter > 30
      set(hh, ...
	  'Xdata',[hx*cos(o)-hy*sin(o)], ...
	  'Ydata',[hx*sin(o)+hy*cos(o)])
      set(hm, ...
	  'Xdata',[mx*cos(m)-my*sin(m)], ...
	  'Ydata',[mx*sin(m)+my*cos(m)])
      counter = 0;
    end
    set(hs1, ...
	'Xdata',[0.05*cos(s) 0.98*cos(s)], ...
	'Ydata',[0.05*sin(s) 0.98*sin(s)])
    set(hs2, ...
	'Xdata',[0.05*cos(pi+s) 0.2*cos(pi+s)], ...
	'Ydata',[0.05*sin(pi+s) 0.2*sin(pi+s)])
  catch
    % clock figure has been likely closed...
    break
  end
  pause(1/8)
  counter = counter + 1;
end